<?php
namespace app\admin\model;
use my\Tools;
use think\Db;
use think\Model;
use app\admin\model\RegionModel;
use my\Security;

class AdminActivity extends Model
{
	 public function __construct()
	{
		parent::__construct();
	}

	//活动列表
    public function getListInfo($page,$limit,$data){
        $where = [];
        //分类乘客司机
        if(strstr($_SERVER['HTTP_REFERER'],"passenger")){
             $where['user_type'] = 1;
         }else{
             $where['user_type'] = 2;
         }
        if ($data['status']!=""){
            switch ($data['status']) {
                case 0:
                    $where['start_time'] = ['>', time()];
                    break;
                case 1:
                    $where['start_time'] = ['<=', time()];
                    $where['end_time'] = ['>=', time()];
                    break;
                case 2:
                    $where['end_time'] = ['<', time()];
                    break;
            }
        }
	     //活动名称搜索
        if (!empty($data['activity_name'])) {
            $where['activity_name'] = ['like','%'.rtrim($data['activity_name']).'%'];
        }
        //活动类型 1首单奖励2叠加活动
        if(!empty($data['activity_model'])){
            $where['activity_model'] = $data['activity_model'];
        }
        //活动开始时间
        if (!empty($data['start_time'])) {
            $where['start_time'] = ['>=',strtotime(rtrim($data['start_time']))];
        }
        //活动结束时间
        if (!empty($data['end_time'])) {
            $where['end_time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
        }
        //所属类型搜索
        if (isset($data['order_type']) && $data['order_type'] != '') {
            $where['order_type'] = $data['order_type'];
        }
        //活动终端
        if (isset($data['user_type']) && $data['user_type'] != '') {
            $where['user_type'] = $data['user_type'];
        }
        //$where['status'] = 1;
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('admin_activity')->where($where)->count();
        $list = Db::name('admin_activity')->where($where)->limit($start, $limit)->order('id','desc')->select();
        $regionList = $adminInfo = $info = [];
        foreach ($list as $key=>$value){
            $list[$key]['user_type']        = terminal($value['user_type']);                                            //活动终端
            $list[$key]['activity_type']    = $value['activity_model'];                                                 //活动类型
            $list[$key]['activity_model']   = acitivetyType($value['activity_model']);                                  //活动类型
            $list[$key]['order_type']       = order_type($value['order_type']);                                         //所属活动
            $list[$key]['start_time']       = !empty($value['start_time'])?date("Y-m-d",$value['start_time']):'';//活动开始时间
            $list[$key]['end_time']         = !empty($value['end_time'])?date("Y-m-d",$value['end_time']):'';   //活动结束时间
            $list[$key]['add_time']         = !empty($value['add_time'])?date("Y-m-d H:i:s",$value['add_time']):'';   //活动创建时间
            if ($value['start_time'] > time()) {
                $list[$key]['status'] = "未开始";
            } elseif (($value['start_time'] <= time()) && ($value['end_time'] >= time())) {
                $list[$key]['status'] = "进行中";
            } else {
                $list[$key]['status'] = "已结束";
            }
            if($value['audit_status']==1){
                $list[$key]['audit_status'] = '<span style="color: green;">已通过</span>';
            }elseif($value['audit_status']==0){
                $list[$key]['audit_status'] = '待审核';
            }
            //城市
            if(!empty($value['region_id'])){
                $regionList = Db::name('region')->where(['region_id'=>['in',$value['region_id']]] )->field('region_name')->select();
                $region_name = '';
                foreach ($regionList as $vml){
                    $region_name .= $vml['region_name'].',';
                }
                $list[$key]['region_name'] = rtrim($region_name,',');
            }else{
                $list[$key]['region_name'] = '全国';
            }
            //创建人
            $adminInfo = getAdminInfo($value['admin_id']);
            $list[$key]['admin_name'] = $adminInfo['user_name'];
            //奖励金额统计
            $info = self::getAcitivitySum($value['id']);
            $list[$key]['rewardMoney'] = $info['rewardMoney'];
            $list[$key]['rewardNumber'] = $info['rewardNumber'];
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }

    //统计发放金额 + 发放人数
    public function getAcitivitySum($activity_id=''){
        $list = Db::name('activity_record')->where(['xm_admin_activity_id'=>$activity_id,'status'=>1,'account_status'=>1] )->field('amount')->select();
        $sumMoney = '';
        if(!empty($list)){
            foreach ($list as $item) {
                $sumMoney += ($item['amount']);
            }
            $data['rewardMoney']    = $sumMoney / 100;//sprintf("%.2f",$sumMoney / 100);
            $data['rewardNumber']   = count($list);
        }else{
            $data['rewardMoney']    = 0;
            $data['rewardNumber']   = 0;
        }
        return $data;
    }

    //活动流水记录详情
    public function getLookList($page,$limit,$data){
        $where = [];
        //活动名称搜索
        if (!empty($data['activity_name'])) {
            $where['b.activity_name'] = ['like','%'.rtrim($data['activity_name']).'%'];
        }
        //活动类型 1首单奖励2叠加活动
        if(!empty($data['activity_model'])){
            $where['a.activity_model'] = $data['activity_model'];
        }
        //活动开始时间
        if (!empty($data['start_time'])) {
            $where['b.start_time'] = ['>=',strtotime(rtrim($data['start_time']))];
        }
        //活动结束时间
        if (!empty($data['end_time'])) {
            $where['b.end_time'] = ['<=',strtotime(rtrim($data['end_time'])." 23:59:59")];
        }
        //到账开始及结束时间
//        account_time
        if (!empty($data['start'])&&!empty($data['end'])) {
            $where['a.account_time'] = ['between',[strtotime(rtrim($data['start'])),strtotime(rtrim($data['end'])." 23:59:59")]];
        }

        //所属类型搜索
        if (isset($data['order_type']) && $data['order_type'] != '') {
            $where['a.order_type'] = $data['order_type'];
        }
        //活动终端
        if (isset($data['user_type']) && $data['user_type'] != '') {
            $where['a.user_type'] = $data['user_type'];
        }
        //手机号搜索
        if (!empty($data['phone'])) {
            $where['a.phone'] = ['like','%'.rtrim($data['phone']).'%'];
        }
        //订单号
        if (!empty($data['order_sn'])) {
            $where['a.order_sn'] = ['like','%'.rtrim($data['order_sn']).'%'];
        }
        //省份ID
        if(!empty($data['province_id']) && empty($data['city_id'])){
            //查询这个省份下面所有城市ID
            $cityList = RegionModel::getRegion(['parent_id'=>$data['province_id'],'region_level'=>2],'region_id');
            $cityId = '';
            if(!empty($cityList)){
                foreach ($cityList as $key=>$value){
                    $cityId .= $value['region_id'].',';
                }
                $where['a.region_id'] = ['in',rtrim($cityId)];
            }
        }elseif(!empty($data['province_id']) && !empty($data['city_id'])){
            $where['a.region_id'] = $data['city_id'];
        }
        $where['a.status'] = 1;
        //分页
        $start = ($page - 1) * $limit;
        $field = 'a.id,a.user_type,a.region_id,a.amount,a.order_type,a.add_time,a.account_status,a.account_time,a.add_time,a.activity_model,a.order_sn,a.phone,b.id as activity_id,b.activity_name,b.start_time,b.end_time';
        $count = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->count();
        if(empty($limit)){
            $limit = $count;
        }
        $sumMoney = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->value('sum(a.amount)');
        $daoZhangSumMoney = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->where(['a.account_status'=>1])->value('sum(a.amount)');
        $list = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->field($field)->limit($start, $limit)->order('a.id','desc')->select();
        $regionList = [];
        foreach ($list as $key=>$value){
            $list[$key]['user_type']        = terminal($value['user_type']);                                                    //活动终端
            $list[$key]['activity_model']   = acitivetyType($value['activity_model']);                                          //活动类型
            $list[$key]['order_type']       = order_type($value['order_type']);                                                 //所属活动
            $list[$key]['start_time']       = !empty($value['start_time'])?date("Y-m-d",$value['start_time']):'';             //活动开始时间
            $list[$key]['end_time']         = !empty($value['end_time'])?date("Y-m-d",$value['end_time']):'';                 //活动结束时间
            $list[$key]['time']             = date("Y-m-d",$value['start_time'])." / ".date("Y-m-d",$value['end_time']);
            $list[$key]['add_time']         = !empty($value['add_time'])?date("Y-m-d H:i:s",$value['add_time']):'';    //流水生成时间
            $list[$key]['account_time']     = !empty($value['account_time'])?date("Y-m-d H:i:s",$value['account_time']):'';  //奖励到账时间
            if($value['account_status'] == 1){
                $list[$key]['accountStatusNameAly']   = "<span style='color: green'>已到账</span>";
                $list[$key]['accountStatusName']   = '已到账';                                                                    //到账状态
            }else{
                $list[$key]['accountStatusName']   = '未到账';                                                                    //到账状态
                $list[$key]['accountStatusNameAly']   = "<span style='color: red'>未到账</span>";
            }
            //城市
            if(!empty($value['region_id'])){
                $regionList = Db::name('region')->where(['region_id'=>['in',$value['region_id']]] )->field('region_name')->select();
                $region_name = '';
                foreach ($regionList as $vml){
                    $region_name .= $vml['region_name'].',';
                }
                $list[$key]['region_name'] = rtrim($region_name,',');
            }else{
                $list[$key]['region_name'] = '-';
            }
            $list[$key]['amount'] = $value['amount'] / 100;//sprintf("%.2f",$value['amount'] / 100);
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list,
            'sumMoney'=>sprintf("%.2f",$sumMoney / 100),
            'daoZhangSumMoney'=>sprintf("%.2f",$daoZhangSumMoney / 100),
            'noZhangSumMoney'=>sprintf("%.2f",($sumMoney - $daoZhangSumMoney) / 100)
        );
    }
    //优惠券发放记录信息
    public function couponDetail($page,$limit,$data){
        $where = [];
        //手机号搜索
        if (!empty($data['user_phone'])) {
            $where['b.user_phone'] = ['like','%'.Security::encrypt(rtrim($data['user_phone'])).'%'];
        }else{
            //$where['b.user_phone'] = ['not null'];
        }
        if(!empty($data['coupon_id'])){
            $where['a.admin_coupon_id'] = $data['coupon_id'];
        }
        $start = ($page - 1) * $limit;
        $field = 'a.coupon_id,a.coupon_price,a.uid,a.valid_start_time,a.valid_end_time,a.is_use,a.region_id,a.order_type,a.admin_active_id,a.admin_coupon_id,a.is_receive,a.receive_time,a.supply_coupon_type,a.time,a.admin_id,b.user_phone';
        $count = Db::name('coupon')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->count();
        $list  = Db::name('coupon')->alias('a')->join('user b','a.uid = b.uid','left')->where($where)->field($field)->limit($start, $limit)->order('a.coupon_id','desc')->select();
        foreach ($list as $key=>$value){
            $list[$key]['user_phone'] = Security::decrypt($value['user_phone']);
            if(!empty($value['valid_start_time'])){     //有效期开始日期
                $list[$key]['valid_start_time'] = date("Y-m-d",$value['valid_start_time']);
            }
            if(!empty($value['valid_end_time'])){       //有效期结束日期
                $list[$key]['valid_end_time'] = date("Y-m-d H:i:s",$value['valid_end_time']);
            }
            if($value['is_use'] == 1){
                $list[$key]['useTypeName'] = "<span style='color:red;'>已使用</span>";
            }else{
                $list[$key]['useTypeName'] = "<span style='color:green;'>未使用</span>";
            }
            if(!empty($value['region_id'])){
                $list[$key]['region_name'] = Db::name('region')->where(['region_id'=>$value['region_id']])->value('region_name');
            }else{
                $list[$key]['region_name'] = '-';
            }
            if($value['is_receive'] == 1){
                $list[$key]['receiveTypeName'] = "<span style='color:green;'>已领取</span>";
            }else{
                $list[$key]['receiveTypeName'] = "<span>未领取</span>";
            }
            if(!empty($value['receive_time'])){     //领取时间
                $list[$key]['receive_time'] = date("Y-m-d H:i:s",$value['receive_time']);
            }
            $list[$key]['order_type']       = order_type($value['order_type']);                                         //所属活动
            //查询优惠券信息
            $couponType = Db::name('admin_coupon')->where(['coupon_id'=>$value['admin_coupon_id']])->value('money_type');
            if($couponType == 1){
                $list[$key]['coupon_price'] = sprintf('%.2f',$value['coupon_price']/100);
                $list[$key]['moneyTypeName'] = '现金券';
            }else{
                $list[$key]['coupon_price'] = $value['coupon_price'] / 10;
                $list[$key]['moneyTypeName'] = '折扣券';
            }
            //系统补发
            if($value['supply_coupon_type'] == 1){
                $list[$key]['supply_coupon_type'] = '系统补发';
            }else{
                $list[$key]['supply_coupon_type'] = '-';
            }
            //系统补发时间
            if(!empty($value['time'])){
                $list[$key]['time'] = date("Y-m-d H:i:s",$value['time']);
            }else{
                $list[$key]['time'] = '-';
            }
            //系统补发人
            if(!empty($value['admin_id'])){
                $list[$key]['adminName'] = getAdminInfo($value['admin_id'],1);
            }else{
                $list[$key]['adminName'] = '-';
            }
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }

    //补发优惠券
    public function supplyCoupon($coupon_id='',$phone=[],$city_id='',$order_type='',$inviter_status=''){
	     //查询该优惠券信息是否存在
        $adminCouponInfo = Db::name('admin_coupon')->where(['coupon_id'=>$coupon_id,'status'=>1])->find();
        if(empty($adminCouponInfo)){
            return ['status'=>0,'msg'=>'该优惠券不存在'];die;
        }
        if($adminCouponInfo['audit_status']!=1){
            return ['status'=>0,'msg'=>'该优惠券活动信息未审核通过,无法补发优惠券'];die;
        }
        //查询用户是否存在
        $userInfo = $userArr = [];
        foreach($phone as $key=>$value){
            $userInfo = Db::name('user')->where(['user_phone'=>Security::encrypt($value),'is_del'=>1])->field('uid')->find();
            if(empty($userInfo)){
                return ['status'=>0,'msg'=>$value.' 手机用户不存在,请确认后提交'];die;
            }else{
                $userArr[] = $userInfo['uid'];
            }
        }
        //判断该优惠券信息 是 新版活动 还剩 老版活动
        if($adminCouponInfo['is_new'] == 1){
            $adminCouponInfo['order_type'] = $order_type;
            //查询该优惠券已经发放总数量已经多少
            $couponNumber = Db::name('coupon')->where(['admin_coupon_id'=>$coupon_id])->count();
            //统计需要补发的用户 总的发多少张
            $supplyCouponNumber = count($userArr) * $adminCouponInfo['limit_num'];
            if(!empty($adminCouponInfo['total_num'])){
                //判断优惠券发行数量是否已经限制数量
                if(($couponNumber + $supplyCouponNumber) > $adminCouponInfo['total_num']){
                    return ['status'=>0,'msg'=>'优惠发行数量已超过当前优惠券设置发行总数'];die;
                }
            }
            //开发发放优惠券
            foreach ($userArr as $key=>$value){
                $couponInfoArr = Db::name('coupon')->where(['admin_coupon_id'=>$coupon_id,'uid'=>$value])->select();//查询优惠券发放信息 是否已发过
                //配合邀请司机活动发优惠券操作
                if(!empty($inviter_status)){
                    $couponInfoArr = [];
                }
                if(!empty($couponInfoArr)){
                    //用户优惠券数量 小于 限领数量 给补上
                    if(count($couponInfoArr) < $adminCouponInfo['limit_num']){
                        $num = $adminCouponInfo['limit_num'] - count($couponInfoArr);
                        for($i=1;$i<=$num;$i++){
                            self::getInsertCoupon($adminCouponInfo,$value,$city_id);
                        }
                    }
                }else{
                    //没有发送优惠券 则补发
                    for($i=1;$i<=$adminCouponInfo['limit_num'];$i++){
                        self::getInsertCoupon($adminCouponInfo,$value,$city_id);
                    }
                }
            }
            return ['status'=>1,'msg'=>'优惠券补发成功'];die;
        }else{
            //开始发送优惠券信息 - 循环用户开始操作
            foreach ($userArr as $key=>$value){
                //$couponInfoArr = Db::name('coupon')->where(['admin_coupon_id'=>$coupon_id,'uid'=>$value])->find();
                //if(empty($couponInfoArr)){
                    //发放优惠券入库操作
                    self::getInsertCoupon($adminCouponInfo,$value,$city_id);
                //}
            }
            return ['status'=>1,'msg'=>'优惠券补发成功'];die;
        }
    }
    //优惠券入库操作
    public function getInsertCoupon($adminCouponInfo=[],$uid,$city_id,$inviter_status=''){
        $reult = [
            'coupon_price'      => $adminCouponInfo['money'],
            'uid'               => $uid,
            'start_time'        => !empty($adminCouponInfo['start_time'])?$adminCouponInfo['start_time']:'',
            'end_time'          => !empty($adminCouponInfo['end_time'])?$adminCouponInfo['end_time']:'',
            'valid_start_time'  => !empty($adminCouponInfo['valid_start_time'])?$adminCouponInfo['valid_start_time']:'',
            'valid_end_time'    => !empty($adminCouponInfo['valid_end_time'])?$adminCouponInfo['valid_end_time']:'',
            'admin_active_id'   => !empty($adminCouponInfo['admin_active_id'])?$adminCouponInfo['admin_active_id']:'',
            'is_use'            => 2,
            'coupon_name'       => $adminCouponInfo['coupon_name'],
            'region_id'         => $city_id,
            'order_type'        => $adminCouponInfo['order_type'],
            'admin_coupon_id'   => $adminCouponInfo['coupon_id'],
            'model'             => $adminCouponInfo['model'],
            'is_receive'        => 1,
            'receive_time'      => time(),
            'is_notice'         => 1,
            'supply_coupon_type'=> 1,
            'admin_id'          => session('admin_id'),
            'time'              => time()
        ];
        if($adminCouponInfo['type_day'] == 2){
            if(empty($adminCouponInfo['start_day'])){
                $time = strtotime(date("Y-m-d"));
            }else{
                $time = strtotime(date("Y-m-d")) + 3600*24*$adminCouponInfo['start_day'];
            }
            $reult['valid_start_time']  = $time;
            $end_time  = $time + 3600*24*$adminCouponInfo['duration_day'];
            $reult['valid_end_time']    = strtotime(date("Y-m-d",$end_time)." 23:59:59");
        }
        Db::name('coupon')->insertGetId($reult);
        //发送短信
        if($adminCouponInfo['money_type']==2){
            $money = $adminCouponInfo['money']/10;
            $coupon_price = $money.'折';
        }else{
            $money = sprintf('%.2f',$adminCouponInfo['money']/100);
            $coupon_price = $money.'元';
        }
        $user_phone = db('user')->where('uid',$uid)->getField('user_phone');
        $user_phone = Security::decrypt($user_phone);
        $content['coupon_price'] = $coupon_price;
        Tools::sendMes($user_phone,$content,37);
    }

    //补发活动金额信息
    public function supplyMoney($activity_id,$phoneArr=[],$city_id='',$time='',$money=''){
        if(empty($phoneArr)){
            return ['status'=>0,'msg'=>'补发手机号码不能为空'];die;
        }
        //查询活动信息
        $activityInfo = Db::name('admin_activity')->where(['id'=>$activity_id])->field('user_type,amount,order_type,activity_model')->find();
        if($activityInfo['user_type'] == 1){    //乘客
            foreach ($phoneArr as $key=>$value) {
                //通过手机号码查询ID
                $userInfo = Db::name('user')->where(['is_del'=>1,'user_phone'=>Security::encrypt($value)])->field('uid')->find();
                if(!empty($userInfo)){
                    $result[$key]['uid']    = $userInfo['uid'];
                    $result[$key]['phone']  = $value;
                }
            }
        }else{                                  //司机
            foreach ($phoneArr as $key=>$value) {
                //通过手机号码查询ID
                $driverInfo = Db::name('driver_base')->where(['driver_phone'=>Security::encrypt($value)])->field('driver_id as uid')->find();
                if(!empty($driverInfo)){
                    $result[$key]['uid']    = $driverInfo['uid'];
                    $result[$key]['phone']  = $value;
                }
            }
        }
        if($activityInfo['activity_model'] == 7){
            $award_type = 4;
        }elseif($activityInfo['activity_model'] == 8){
            $award_type = 5;
        }elseif($activityInfo['activity_model'] == 6){
            $award_type = 1;
        }elseif($activityInfo['activity_model'] == 2){
            $award_type = 2;
        }else{
            $award_type = 0;
        }
        if(empty($result)){
            return ['status'=>0,'msg'=>'补发人员手机号码不正确'];die;
        }
        $activityInfo['amount'] = !empty($money)?$money:$activityInfo['amount'];
        //奖励记录入库
        foreach ($result as $key=>$value){
            //判断该用户是否已经发放奖励
            $recordInfo = Db::name('activity_record')->where(['xm_admin_activity_id'=>$activity_id,'uid'=>$value['uid']])->find();
            //if(empty($recordInfo)){
                $activityRecord = [
                    'xm_admin_activity_id'  => $activity_id,                    'user_type'        => $activityInfo['user_type'],
                    'uid'                   => $value['uid'],                   'region_id'        => $city_id,
                    'amount'                => $activityInfo['amount'],         'order_type'       => $activityInfo['order_type'],
                    'add_time'              => $time,                           'account_status'   => 1,
                    'account_time'          => $time,                           'notice_status'    => 1,
                    'notice_time'           => $time,                           'status'           => 1,
                    'activity_model'        => $activityInfo['activity_model'], 'phone'            => $value['phone'],
                ];
                //奖励记录入库
                Db::name('activity_record')->insertGetId($activityRecord);
                if($activityInfo['user_type'] == 1){    //乘客 - 奖励流水记录 + 账户余额增加
                    $data = [
                        'uid'               => $value['uid'],                   'money'            => $activityInfo['amount'],
                        'money_statement'   => 12,                              'time'             => $time,
                        'status'            => 1
                    ];
                    Db::name('user_recharge')->insertGetId($data);
                    Db::name('user_wallet')->where(['uid'=>$value['uid']])->setInc('wallet_money',$activityInfo['amount']);
                }else{                                  //司机 - 奖励流水记录 + 账户余额增加
                    //查询该司机是否存在钱包信息
                    $driverMoney = Db::name('driver_money')->where(['driver_id'=>$value['uid']])->find();
                    if(empty($driverMoney)){
                        $wallet_id = Db::name('driver_money')->insertGetId(['driver_id'=>$value['uid'],'money'=>$activityInfo['amount'],'award'=>$activityInfo['amount'],'award_balance'=>$activityInfo['amount']]);
                    }else{
                        $wallet_id = $driverMoney['id'];
                        Db::name('driver_money')->where(['driver_id'=>$value['uid']])->setInc('money',$activityInfo['amount']);
                        Db::name('driver_money')->where(['driver_id'=>$value['uid']])->setInc('award',$activityInfo['amount']);
                        Db::name('driver_money')->where(['driver_id'=>$value['uid']])->setInc('award_balance',$activityInfo['amount']);
                    }
                    //生成流水记录
                    $driver_wallet_record = [
                        'wallet_id'     => $wallet_id,
                        'driver_id'     => $value['uid'],
                        'record_type'   => 4,
                        'money'         => $activityInfo['amount'],
                        'time'          => $time,
                        'status'        => 1,
                        'award_type'    => $award_type
                    ];
                    Db::name('driver_wallet_record')->insertGetId($driver_wallet_record);
                }
            //}
        }
        return ['status'=>1,'msg'=>'补发成功'];die;
    }
    //奖励数据明细
    public function rewardList($page,$limit,$data=[]){
        if(!empty($data['uid'])){
            $where['a.uid'] = $data['uid'];
        }
        if(!empty($data['user_type'])){
            $where['a.user_type'] = $data['user_type'];
        }
        //活动名称搜索
        if (!empty($data['activity_name'])) {
            $where['b.activity_name'] = ['like','%'.rtrim($data['activity_name']).'%'];
        }
        //活动类型
        if(!empty($data['activity_model'])){
            $where['a.activity_model'] = $data['activity_model'];
        }
        //到账时间
        if (!empty($data['startTime']) && empty($data['endTime'])) {
            $where['a.account_time'] = ['>=',strtotime(rtrim($data['startTime']))];
        }elseif(empty($data['startTime']) && !empty($data['endTime'])){
            $where['a.account_time'] = ['<=',strtotime(rtrim($data['endTime'])." 23:59:59")];
        }elseif(!empty($data['startTime']) && !empty($data['endTime'])){
            $where['a.account_time'] = ['between',[strtotime(rtrim($data['startTime'])),strtotime(rtrim($data['endTime'])." 23:59:59")]];
        }
        $field = 'a.id,a.xm_admin_activity_id,a.amount,a.account_time,a.activity_model,a.order_sn,b.activity_name';
        $start = ($page - 1) * $limit;
        $list = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->field($field)->limit($start, $limit)->select();
        $count = Db::name('activity_record')->alias('a')->join('admin_activity b','a.xm_admin_activity_id = b.id','left')->where($where)->limit($start, $limit)->count();
        foreach ($list as $key=>$value){
            $list[$key]['amount']        = $value['amount']/100;
            $list[$key]['account_time']  = !empty($value['account_time'])?date("Y-m-d H:i:s",$value['account_time']):'';
            $list[$key]['modelName']     = acitivetyType($value['activity_model']);
            $list[$key]['typeNmae']      = '账户余额';
        }
        return array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $list
        );
    }
}
